package com.chujian.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.chujian.DBUtil.DBUtil;
import com.chujian.dao.OrderDao;
import com.chujian.model.Comment;
import com.chujian.model.Order;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class OrderDaoImpl implements OrderDao{

	@Override
	public boolean addOrder(Order order) {
		String sql=" insert into order1(order_id,order_time,order_status,table_name,user_phone,total_price,cancel_flag)values(?,?,?,?,?,?,?) ";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, order.getOrderId());
			ptmt.setString(2, order.getOrderTime());
			ptmt.setInt(3, order.getOrderStatus());
			ptmt.setString(4, order.getTableName());
			ptmt.setString(5, order.getUserPhone());
			ptmt.setFloat(6, order.getTotalPrice());
			ptmt.setInt(7, 0);
			
			int count=ptmt.executeUpdate();
			if(count>0){
				return true;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		return false;
	}

	@Override
	public boolean deleteOrder(String orderId) {
		// TODO Auto-generated method stub
		String sql=" delete from order1 where order_id=? ";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, orderId);
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

	@Override
	public boolean updateOrder(Order order) {
		// TODO Auto-generated method stub
		
		
		
		return false;
	}

	@Override
	public List<Order> qeuryAll() {
		// TODO Auto-generated method stub
		String sql=" select * from order1  ORDER BY order_id DESC";
		Connection conn=DBUtil.getConnection();
		
		List<Order> list=new ArrayList<Order>();
		
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
				int orderStatus=rs.getInt("order_status");
				int tableId=rs.getInt("table_id");
				int userId=rs.getInt("user_id");
				int foodListId=rs.getInt("food_list_id");
				int commentFlag=rs.getInt("comment_flag");
				float totalPrice=rs.getFloat("total_price");
				int cancelFlag=rs.getInt("cancel_flag");
//				Order order=new Order(orderId, orderTime, orderStatus, tableId, userId, foodListId, commentFlag, totalPrice);
//				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public Order qeuryByOrderId(String orderId) {
		// TODO Auto-generated method stub
		String sql=" select * from order1 where order_id=? ";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, orderId);
			ResultSet rs=ptmt.executeQuery();
			if(rs.next()){
//				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
				int orderStatus=rs.getInt("order_status");
				String tableName=rs.getString("table_name");
				String userPhone=rs.getString("user_phone");
				float totalPrice=rs.getFloat("total_price");
				int cancelFlag=rs.getInt("cancel_flag");
				Order order=new Order(orderId, orderTime, orderStatus, tableName, userPhone, totalPrice);
				order.setCancelFlag(cancelFlag);
				return order;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return null;
	}

	@Override
	public List<Order> qeuryByTableName(String tableName) {
		// TODO Auto-generated method stub
		String sql=" select * from order1 where table_name=?  ORDER BY order_id DESC";
		Connection conn=DBUtil.getConnection();
		
		List<Order> list=new ArrayList<Order>();
		
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, tableName);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
				int orderStatus=rs.getInt("order_status");
				float totalPrice=rs.getFloat("total_price");
				
				String userPhone=rs.getString("user_phone");
				int cancelFlag=rs.getInt("cancel_flag");
//				String tableName=rs.getString("table_name");
				Order order=new Order(orderId, orderTime, orderStatus, tableName, userPhone, totalPrice);
				order.setCancelFlag(cancelFlag);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public List<Order> qeuryByUserPhone(String  userPhone) {
		// TODO Auto-generated method stub
		String sql=" select * from order1 where user_phone=?  ORDER BY order_id DESC";
		Connection conn=DBUtil.getConnection();
		
		List<Order> list=new ArrayList<Order>();
		
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, userPhone);
			ResultSet rs=ptmt.executeQuery();
			if(rs.next()){
				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
				int orderStatus=rs.getInt("order_status");
				float totalPrice=rs.getFloat("total_price");
//				String userPhone=rs.getString("user_phone");
				String tableName=rs.getString("table_name");
				int cancelFlag=rs.getInt("cancel_flag");
				Order order=new Order(orderId, orderTime, orderStatus, tableName, userPhone, totalPrice);
				order.setCancelFlag(cancelFlag);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public List<Order> qeuryByOrderStatus(int orderStatus) {
		// TODO Auto-generated method stub
		String sql=" select * from order1 where order_status=?  ORDER BY order_id DESC";
		Connection conn=DBUtil.getConnection();
		
		List<Order> list=new ArrayList<Order>();
		
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setInt(1, orderStatus);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
//				int orderStatus=rs.getInt("order_status");
				float totalPrice=rs.getFloat("total_price");
				String userPhone=rs.getString("user_phone");
				String tableName=rs.getString("table_name");
				int cancelFlag=rs.getInt("cancel_flag");
				Order order=new Order(orderId, orderTime, orderStatus, tableName, userPhone, totalPrice);
				order.setCancelFlag(cancelFlag);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public List<Order> qeuryByPhoneAndStatus(String userPhone, int orderStatus) {
		String sql=" select * from order1 where user_phone=? and order_status=?  ORDER BY order_id DESC";
		Connection conn=DBUtil.getConnection();
		
		List<Order> list=new ArrayList<Order>();
		
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, userPhone);
			ptmt.setInt(2, orderStatus);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
//				int orderStatus=rs.getInt("order_status");
				float totalPrice=rs.getFloat("total_price");
//				String userPhone=rs.getString("user_phone");
				String tableName=rs.getString("table_name");
				int cancelFlag=rs.getInt("cancel_flag");
				Order order=new Order(orderId, orderTime, orderStatus, tableName, userPhone, totalPrice);
				order.setCancelFlag(cancelFlag);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public boolean cancelOrder(String orderId) {
		// TODO Auto-generated method stub
		String sql="update order1 set order_status=? where order_id=?";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setInt(1,3);
			ptmt.setString(2, orderId);
			
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

	@Override
	public boolean setOrderGone(String orderId, int orderStatus) {
		// TODO Auto-generated method stub
		String sql="update order1 set order_status=? where order_id=?";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setInt(1,orderStatus);
			ptmt.setString(2, orderId);
			
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

	@Override
	public boolean updateTotalPrice(String orderId, float orderPrice) {
		// TODO Auto-generated method stub
		String sql="update order1 set total_price=?where order_id=?";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setFloat(1, orderPrice);
			ptmt.setString(2, orderId);
			
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

	@Override
	public List<Order> qeuryByTime(long startTime, long endTime) {
		String sql=" select * from order1  where order_time>? and order_time<? and order_status=1";
		Connection conn=DBUtil.getConnection();
		List<Order> list=new ArrayList<Order>();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setLong(1, startTime);
			ptmt.setLong(2, endTime);
			ResultSet rs=ptmt.executeQuery();
			while(rs.next()){
				String orderId=rs.getString("order_id");
				String orderTime=rs.getString("order_time");
				int orderStatus=rs.getInt("order_status");
				float totalPrice=rs.getFloat("total_price");
				
				String userPhone=rs.getString("user_phone");
				String tableName=rs.getString("table_name");
				int cancelFlag=rs.getInt("cancel_flag");
				Order order=new Order(orderId, orderTime, orderStatus, tableName, userPhone, totalPrice);
				order.setCancelFlag(cancelFlag);
				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		
		
		return list;
	}

	@Override
	public boolean cancelRequestFlag(String orderId) {
		// TODO Auto-generated method stub
				String sql="update order1 set cancel_flag=? where order_id=?";
				Connection conn=DBUtil.getConnection();
				try {
					PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
					ptmt.setInt(1,1);
					ptmt.setString(2, orderId);
					
					int count=ptmt.executeUpdate();
					if(1==count){
						return true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally{
					DBUtil.closeConn(conn);
				}
				return false;
	}

	@Override
	public boolean modifyTable(String orderId, String tableName) {
		String sql="update order1 set table_name=? where order_id=?";
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement ptmt=(PreparedStatement) conn.prepareStatement(sql);
			ptmt.setString(1, tableName);
			ptmt.setString(2, orderId);
			
			int count=ptmt.executeUpdate();
			if(1==count){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeConn(conn);
		}
		return false;
	}

}



